package com.kbbywyclient.ui.activity;

import android.webkit.WebSettings;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.data.entity.Event;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.util.JSInterface;
import com.kbbywyclient.util.LogUtil;
import com.kbbywyclient.util.RsaJsonUtil;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.CustomToolBar;
import com.kbbywyclient.widget.ProgressWebView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * Description: 贷款 表单详情
 * Created by FHW
 * on 2017/4/1
 */

public class LoansTableDetailActivity extends BaseActivity {

    @BindView(R.id.v_toolbar)
    CustomToolBar v_toolbar;
    @BindView(R.id.wv_contact)
    ProgressWebView wv_contact;

    private RsaJsonUtil rsaJsonUtil;

    private int applyLoansID = -1;
    private String applyLoansSN = "";
    private String params = "";

    private int position = 0;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_credit_table_detail;
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("贷款-客户申请表详情");
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);

        rsaJsonUtil = new RsaJsonUtil();

        applyLoansID = getIntent().getIntExtra("ApplyLoansID", -1);
        applyLoansSN = getIntent().getStringExtra("ApplyLoansSN");
        position = getIntent().getIntExtra("position", 0);

        v_toolbar.setLeftViewOnClickLinster(v -> finish());

        //设置webview
        wv_contact.getSettings().setDefaultTextEncodingName("UTF-8");
        //webview自适应屏幕
        WebSettings settings = wv_contact.getSettings();
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        //                    settings.setBuiltInZoomControls(true); //显示放大缩小
        //                    settings.setSupportZoom(true); //可以缩放
        // webview自适应第二种
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        wv_contact.setSaveEnabled(true);
        settings.setJavaScriptEnabled(true); // 设置支持javascript脚本

        // 添加js交互接口类，并起别名 jsInterface
        wv_contact.addJavascriptInterface(new JSInterface(this), "jsInterface");

        rsaJsonUtil.put("SalesID", PreUtils.getUser(this).getSalesID() + "");
        rsaJsonUtil.put("ApplyLoansID", applyLoansID + "");
        rsaJsonUtil.put("ApplyLoansSN", applyLoansSN);
        params = rsaJsonUtil.getRsaParams();
        String url = API.LOANS_DETAIL + "?params=" + params;
        LogUtil.d(url);
        wv_contact.loadUrl(url);

    }

    @Override
    public void initData() {

    }

    @Override
    public void initAppComponent(AppComponent appComponent) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 购买 回调
     *
     * @param updateCustomerTableDetail
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event.UpdateCustomerTableDetail updateCustomerTableDetail) {
        wv_contact.reload();
        EventBus.getDefault().post(new Event.NotifyCustomerTable(position));
        setScreenAction("购买", "贷款-客户申请表详情-购买");
    }

    /**
     * @param detailMobile
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event.DetailMobile detailMobile) {
        setScreenAction("通信", "贷款-客户申请表详情-拨打电话");
    }

}
